<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Loader;
use think\Validate;
use \app\admin\model\RegistrActivity as RegistrActivitys;
use \app\admin\model\RegistrActivityUser;
use think\Db;

class Registractivity extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    protected $hmodel      = null;
    protected $user_id     = 0;

    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->id;
    }

    /**
     * [getRegistrActivity 獲取掃碼活動詳情]
     * @MethodName
     * @Author     Allen
     * @DateTime   2022-07-21T00:06:15+0800
     * @return     [type]                   [description]
     */
    public function getRegistrActivity()
    {
        $rq_data  = $this->request->param();
        $validate = Loader::validate('RegistrActivity');
        if (!$validate->scene('info')->check($rq_data)) {
            $this->error($validate->getError());
        }
        if (iconv_strlen($rq_data['code']) < 3) {
            $this->error('二維碼錯誤');
        }
        $code = explode('_', decode($rq_data['code']));

        if (!isset($code[2])) {
            $this->error('二維碼錯誤');
        }

        $id    = $code[2];
        $model = new RegistrActivitys;

        $info = $model
            ->field('id,name,content,status,createtime')
            ->where(['id' => $id])
            ->find();

        if (!$info) {
            $data               = [];
            $data['id']         = 0;
            $data['name']       = '';
            $data['content']    = '';
            $data['createtime'] = 0;
            $data['status']     = 2;
            $this->success('請求成功', $data);
        }
        $this->success('請求成功', $info);
    }
    public function category()
    {
     
        $list = Db::name('activity_register__category')->where('deletetime',null)->order('weigh desc')->select();

      
        $this->success('請求成功', $list);
    }
    /**
     * [getLastSignupData 獲取上一次簽到信息]
     * @MethodName
     * @Author     Allen
     * @DateTime   2022-07-21T00:19:00+0800
     * @return     [type]                   [description]
     */
    public function getLastSignupData()
    {
        $model = new RegistrActivityUser;

        $info = $model
        // ->field('name,sex,id_card,birthday,certificate_no,mobile,consultation_content')
        ->field(['id', 'registr_activity_id', 'user_id', 'createtime', 'updatetime', 'deletetime'], true)
            ->where(['user_id' => $this->user_id])
            ->order('id desc')
            ->find();

            $info['birthday']=substr($info['birthday'],0,4).'年';
            $info['consultation_type_text']=db::name('activity_register__category')->where('id',$info['consultation_type'])->value('name');

        // if ($info) {
        //     $info = $info->toArray();
        // } else {
        //     $fields                   = $model->getTableFields();
        //     $info = [];
        //     foreach ($fields as $key => $value) {
        //         if (in_array($value, ['id','registr_activity_id','user_id','createtime','updatetime','deletetime'])) {
        //             continue;
        //         }
        //         $info[$value] = '';
        //     }
        // }

        // if (isset($info['id_card'])) {
        //     $info['id_card'] = str_replace('(', '', $info['id_card']);
        //     $info['id_card'] = str_replace(')', '', $info['id_card']);
        //     $info['id_card'] = str_replace('（', '', $info['id_card']);
        //     $info['id_card'] = str_replace('）', '', $info['id_card']);
        // }

        $this->success('請求成功', $info ? $info : []);
    }

    /**
     * [signup 签到]
     * @MethodName
     * @Author     Allen
     * @DateTime   2022-07-21T00:14:08+0800
     * @param      string                   $value [description]
     * @return     [type]                          [description]
     */
    public function signup($value = '')
    {
        $rq_data  = $this->request->param();
        $validate = Loader::validate('RegistrActivity');
        if (!$validate->scene('signup')->check($rq_data)) {
            $this->error($validate->getError());
        }
        if (iconv_strlen($rq_data['code']) < 3) {
            $this->error('二維碼錯誤');
        }
        $code = explode('_', decode($rq_data['code']));

        if (!isset($code[2])) {
            $this->error('二維碼錯誤');
        }

        $id    = $code[2];
        $model = new RegistrActivitys;

        $info = $model
            ->field('id,name,status,createtime')
            ->where(['id' => $id])
            ->find();

        if (!$info) {
            $this->error('二維碼錯誤');
        }

        $urmodel = new RegistrActivityUser;

        $insert_arr                        = [];
        $insert_arr['registr_activity_id'] = $info['id'];
        $insert_arr['user_id']             = $this->user_id;
        $insert_arr['name']                = $rq_data['name'];
        $insert_arr['gender']              = isset($rq_data['gender']) ? $rq_data['gender'] : 0;
        $insert_arr['id_card']             = isset($rq_data['id_card']) ? $rq_data['id_card'] : '';
        $insert_arr['birthday']            = isset($rq_data['birthday']) ? $rq_data['birthday'] : '';
        $insert_arr['return_permit']       = isset($rq_data['return_permit']) ? $rq_data['return_permit'] : '';
        $insert_arr['hk_address']          = isset($rq_data['hk_address']) ? $rq_data['hk_address'] : '';
        $insert_arr['nd_address']          = isset($rq_data['nd_address']) ? $rq_data['nd_address'] : '';
        $insert_arr['hk_mobile']           = isset($rq_data['hk_mobile']) ? $rq_data['hk_mobile'] : '';
        $insert_arr['nd_mobile']           = isset($rq_data['nd_mobile']) ? $rq_data['nd_mobile'] : '';
        $insert_arr['email']               = isset($rq_data['email']) ? $rq_data['email'] : '';
        $insert_arr['fm_mobile']           = isset($rq_data['fm_mobile']) ? $rq_data['fm_mobile'] : '';
        $insert_arr['native_place']        = isset($rq_data['native_place']) ? $rq_data['native_place'] : '';
        $insert_arr['education']           = isset($rq_data['education']) ? $rq_data['education'] : '1';
        $insert_arr['cognitive_approach']  = isset($rq_data['cognitive_approach']) ? $rq_data['cognitive_approach'] : '1';
        $insert_arr['is_voter']            = isset($rq_data['is_voter']) ? $rq_data['is_voter'] : '2';
        $insert_arr['trade_union']         = isset($rq_data['trade_union']) ? $rq_data['trade_union'] : '';
        $insert_arr['native_place']        = isset($rq_data['native_place']) ? $rq_data['native_place'] : '';
        $insert_arr['consultation_content'] = isset($rq_data['consultation_content']) ? $rq_data['consultation_content'] : '';
          $insert_arr['consultation_type'] = isset($rq_data['consultation_type']) ? $rq_data['consultation_type'] :0;
        $insert_arr['remark']               = isset($rq_data['remark']) ? $rq_data['remark'] : '';
        $insert_arr['createtime']           = time();
        $status                             = $urmodel->insert($insert_arr);

        if (!$status) {
            $this->error('操作失敗');
        }

        $model->where(['id' => $info['id']])->setInc('join_nun');

        $this->success('操作成功');
    }
}
